//
//  NewFeatureController.m
//  Project
//
//  Created by 张强 on 16/9/30.
//  Copyright © 2016年 张强. All rights reserved.
//

#import "NewFeatureController.h"

@interface NewFeatureController ()<UIScrollViewDelegate>

/*!
 * 分页控件
 */
@property (nonatomic,weak)UIPageControl *pageControl;

@property(nonatomic,weak)UIScrollView *scrolllView;

@end

@implementation NewFeatureController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //创建scrollView
    UIScrollView *scrollView  = [[UIScrollView alloc]initWithFrame: self.view.bounds];
    
    //禁止超出contentSize的范围
    scrollView.bounces = NO;
    //开启分页
    scrollView.pagingEnabled = YES;
    //去除水平方向滚动条
    scrollView.showsHorizontalScrollIndicator = NO;
    
    //监听滑动
    scrollView.delegate = self;
    
    //图片个数
    NSInteger count= 4;
    
    for (int i=0; i<count; i++)
    {
        
        UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"guide_40_%d",i+1]];
        
        
        UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
        //设置imageView的大小
        imgView.size = scrollView.size;
        imgView.x = i * scrollView.width;
        
        [scrollView addSubview:imgView];
        
        if (i == count-1) {
            //最后一页
            [self setupLastPageWithView:imgView];
        }
        
    }
    //设置scrollView的内容大小
    [scrollView setContentSize:CGSizeMake(count * scrollView.width, 0)];
    [self.view addSubview:scrollView];
    
    _scrolllView = scrollView;
    
    
    /*
     添加分页控件
     1. 创建分页控件
     1. 分页控件的位置
     2. 分页控件选中与默认颜色(如没有,则显示为白色)
     2. 分页控件和UIScrollView关联
     1. 遵循代理
     2. 设置当前为代理类(滑动时,分页控件改变)
     3. 实现代理方法(滑动时,改变分页控件)
     */
    /**/
    UIPageControl * pageControl = [[UIPageControl alloc]init];
    //设置显示几页
    pageControl.numberOfPages = count;
    //设置选中的颜色与默认的颜色
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    
    
    //设置位置
    pageControl.centerX = self.view.width*0.5;
    pageControl.y = self.view.height - 100;
    self.pageControl = pageControl;
    [self.view addSubview:pageControl];
    
}

//设置最后页视图
- (void)setupLastPageWithView:(UIImageView *)imageView
{
    
    imageView.userInteractionEnabled = YES;
    
    //背景图片
    UIButton *enterButton = [[UIButton alloc]init];
    //正常状态图片按钮
    [enterButton setBackgroundImage:[UIImage imageNamed:@"icon_next"]forState:UIControlStateNormal];
    
    //设置按钮大小(currentBackgroundImage - 背景图片)
    enterButton.size = enterButton.currentBackgroundImage.size;
    //设置按钮位置
    enterButton.centerX = imageView.width * 0.5;
    enterButton.y = imageView.height - 80;
    
    //成为imageView的子类
    [imageView addSubview:enterButton];
    
    [enterButton addTarget:self action:@selector(enterBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
}
//按钮点击事件
-(void)enterBtnClick:(UIButton*)button
{
    //这里显示主页面
    //AXFTabBarController *mainTabBarController = [AXFTabBarController new];
    Class cls = NSClassFromString(@"AXFTabBarController");
    UIViewController *baseVC = [[cls alloc] init];
    
    [UIView animateWithDuration:1 animations:^{
        _scrolllView.alpha = 0;
    } completion:^(BOOL finished) {
        [UIApplication sharedApplication].keyWindow.rootViewController = baseVC;
    }];
}
// 已经滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //contentOffset 偏移量
    double page = scrollView.contentOffset.x /scrollView.width;
    //指定当前页所展示的当前页面(滑动到一半位置,则显示下一页)
    self.pageControl.currentPage = (int)(page+0.5);
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
